import { Widget, _decorator, Node, PageView, tween, v3 } from 'cc';
import { UIBase } from '../scriptBase/UIBase';
import { UI } from '../enum/UI';
import { GameMgr } from '../manager/GameMgr';
import { CoinItem } from '../uiItem/CoinItem';
import { CoinMgr } from '../manager/CoinMgr';
import { AudioMgr } from '../manager/AudioMgr';
import { ArrayUtil } from '../util/ArrayUtil';
import { CfgLoginReward } from '../config/CfgLoginReward';
import { StorageUtil } from '../util/StorageUtil';
import { GameUtil } from '../util/GameUtil';
const { ccclass, property, requireComponent } = _decorator;

@ccclass('UI/UIMain')
@requireComponent(Widget)
export class UIMain extends UIBase {

    @property(Node)
    public sideNode: Node = null;

    private pageView: PageView = null
    private coinItem: CoinItem = null
    private hand: Node = null

    protected onLoad(): void {
        this.pageView = this.findComp('PageView', PageView)
        this.coinItem = this.findComp('CoinItem', CoinItem)
        this.hand = this.findNode('Tip/Hand')

        
    }

    protected start(): void {
        tween(this.hand).to(1, { position: v3(200, 0, 0) }).to(1, { position: v3(-200, 0, 0) }).union().repeatForever().start()
    }

    public onOpen(data?: any): void {
        this.pageView.scrollToPage(GameMgr.mode, 0)
        //this.coinItem.init(CoinMgr.CurCoin)
        AudioMgr.playBgm('主界面音乐')

        GameMgr.gameCampWin = [0,0,0];
    }

    public onClose(data?: any): void {
    }

    protected onBtnSettingClick(): void {
        this.open(UI.Setting)
    }

    protected onBtnLuckySpinClick(): void {
        this.open(UI.LuckySpin)
    }

    protected onBtnPassRewardClick(): void {
        this.open(UI.PassReward)
    }

    //图鉴
    protected onBtnShopClick(): void {
        this.open(UI.ShopView)
    }

    protected onBtnSideClick(): void {
        this.sideNode.active = true
    }

    //今日
    protected onBtnTodayClick(): void {
        if (GameMgr.gameData.todayRewardDay == GameUtil.getYearToday()){
            this.open(UI.Toast, '今日福利已领取')
            return;
        }
        this.open(UI.GetReward, [ArrayUtil.pickItem(CfgLoginReward)])

        GameMgr.gameData.todayRewardDay = GameUtil.getYearToday()
        GameMgr.saveGameUserData();
    }

    //限时
    protected onBtnLimitClick(): void {
        this.open(UI.LoginReward)
    }

}


